/*
 *    Copyright (c) 2018-2025, cloud All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: cloud
 */
package com.fuyao.cloud.space.roomland.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuyao.cloud.common.core.constant.CommonConstants;
import com.fuyao.cloud.common.core.util.DictItemOption;
import com.fuyao.cloud.common.core.util.KeyValue;
import com.fuyao.cloud.common.core.util.R;
import com.fuyao.cloud.common.data.datascope.DataScope;
import com.fuyao.cloud.common.data.resolver.DictItemResolver;
import com.fuyao.cloud.common.data.resolver.NumberResolver;
import com.fuyao.cloud.space.constant.LandConstant;
import com.fuyao.cloud.space.entity.RoomLand;
import com.fuyao.cloud.space.entity.RoomRegional;
import com.fuyao.cloud.space.regional.service.RoomRegionalService;
import com.fuyao.cloud.space.roomland.mapper.RoomLandMapper;
import com.fuyao.cloud.space.roomland.service.RoomLandService;
import com.fuyao.cloud.space.vo.RoomLandExcelVo;
import lombok.AllArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 房屋模块
 *
 * @author 时间的朋友
 * @date 2021-11-09 15:19:15
 */
@Service
@AllArgsConstructor
public class RoomLandServiceImpl extends ServiceImpl<RoomLandMapper, RoomLand> implements RoomLandService {

    private RoomRegionalService roomRegionalService;

    @Override
    public boolean save(RoomLand entity) {
        beforeSave(entity);
        return super.save(entity);
    }

    @Override
    public List<RoomLandExcelVo> listRoomLandExcel(QueryWrapper queryWrapper) {
        List<RoomLand> roomLandList = this.baseMapper.selectListByScope(queryWrapper, new DataScope());
        Map<String, List<DictItemOption>> resultMap = DictItemResolver.getDictOptionResultMap("room_land_status");
        if (CollUtil.isNotEmpty(roomLandList)) {
            return roomLandList.stream().map(roomLand -> {
                RoomLandExcelVo excelVo = new RoomLandExcelVo();
                BeanUtil.copyProperties(roomLand, excelVo);
                excelVo.setStatus(DictItemResolver.getDictOptionLabel(resultMap.get("room_land_status"), roomLand.getStatus()));
                return excelVo;
            }).collect(Collectors.toList());
        }
        return Lists.newArrayList();
    }

    @Override
    public List<KeyValue<Long, String>> listIdAndAssetName() {
        return this.baseMapper.listIdAndAssetName();
    }

    @Transactional
    @Override
    public R batchDelete(List<Long> ids) {
        StringBuilder msg = StrUtil.builder();
        List<RoomRegional> roomRegionalList = roomRegionalService.listByRoomLandId(ids);
        if (CollUtil.isNotEmpty(roomRegionalList)) {
            String landName = CollUtil.join(roomRegionalList.stream().map(RoomRegional::getLandName).distinct().collect(Collectors.toList()), ",");
            msg.append("以下土地信息已关联区域:【").append(landName).append("】无法删除");

            List<Long> removeLandIds = roomRegionalList.stream().map(RoomRegional::getLandId).filter(landId -> !ids.contains(landId)).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(removeLandIds)) {
                this.removeByIds(ids);
                msg.append("\n成功删除").append(ids.size()).append("条数据");
            }
            return R.ok(CommonConstants.SUCCESS, msg.toString());
        }

        boolean remove = this.removeByIds(ids);
        if (remove) {
            return R.ok(CommonConstants.SUCCESS, String.format("成功删除%s条数据",(ids.size())));
        }
        return R.failed(ids,"删除失败");
    }

    private void beforeSave(RoomLand entity) {
        if (StrUtil.isEmpty(entity.getAssetNumber())) {
            entity.setAssetNumber(NumberResolver.getNumberStr(LandConstant.DEFAULT_TYPE, LandConstant.DEFAULT_CODE).getData());
        }
    }
}
